package guoanmaker.operator.business.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import guoanmaker.operator.business.model.AppOutServerClass;
import guoanmaker.operator.business.model.OperatorCompanyDevelopServer;
import guoanmaker.operator.business.model.OperatorEventDetails;
import guoanmaker.operator.business.model.OperatorFriendshipLink;
import guoanmaker.operator.business.model.OperatorHotBusiness;
import guoanmaker.operator.business.model.OperatorHotwords;
import guoanmaker.operator.business.model.OperatorOfficeSpace;
import guoanmaker.operator.business.model.OperatorPcActive;
import guoanmaker.operator.business.model.OperatorPcCompanyDevelop;
import guoanmaker.operator.business.model.OperatorRoundPhoto;
import guoanmaker.operator.business.model.OperatorServerClass;
import guoanmaker.operator.business.model.OperatorWelcomes;
import guoanmaker.operator.business.model.PctwoServerClass;
import guoanmaker.operator.business.model.ProvideRoom;
import guoanmaker.operator.business.model.ProviderBusiness;
import guoanmaker.operator.business.modelnobind.OperatorHotBusinessRo;
import guoanmaker.operator.business.modelnobind.OperatorOfficeSpaceRo;
import guoanmaker.operator.business.modelnobind.OperatorPcActiveRo;
import guoanmaker.operator.business.repository.OperatorCompanyDevelopServerRepository;
import guoanmaker.operator.business.repository.OperatorEventDetailsRepository;
import guoanmaker.operator.business.repository.OperatorFriendshipLinkRepository;
import guoanmaker.operator.business.repository.OperatorHotBusinessRepository;
import guoanmaker.operator.business.repository.OperatorHotwordsRepository;
import guoanmaker.operator.business.repository.OperatorOfficeSpaceRepository;
import guoanmaker.operator.business.repository.OperatorPcActiveRepository;
import guoanmaker.operator.business.repository.OperatorPcCompanyDevelopRepository;
import guoanmaker.operator.business.repository.OperatorRoomRepository;
import guoanmaker.operator.business.repository.OperatorRoundPhotoRepository;
import guoanmaker.operator.business.repository.OperatorServerClassRepository;
import guoanmaker.operator.business.repository.OperatorWelcomesRepository;
import guoanmaker.operator.business.repository.ProviderBusinessRepository;

@Service
public class OperatoroOficialService implements OperatoroOficialSeviceInterface{
	@Autowired
	private OperatorHotwordsRepository operatorHotwordsRepository;
	@Autowired
	private  OperatorRoundPhotoRepository operatorRoundPhotoRepository;
	@Autowired
	private  OperatorWelcomesRepository operatorWelcomesRepository;
	@Autowired
	private OperatorServerClassRepository operatorServerClassRepository;
	@Autowired
	private OperatorOfficeSpaceRepository operatorOfficeSpaceRepository;
	@Autowired
	private OperatorRoomRepository operatorRoomRepository;
	@Autowired
	private OperatorCompanyDevelopServerRepository operatorCompanyDevelopServerRepository;
	@Autowired
	private OperatorPcCompanyDevelopRepository operatorPcCompanyDevelopRepository;
	@Autowired
	private OperatorPcActiveRepository operatorPcActiveRepository;
	@Autowired
	private OperatorEventDetailsRepository operatorEventDetailsRepository;
	@Autowired
	private OperatorHotBusinessRepository operatorHotBusinessRepository;
	@Autowired
	private ProviderBusinessRepository providerBusinessRepository;
	@Autowired
	 private  OperatorFriendshipLinkRepository operatorFriendshipLinkRepository;
	@Override
	public Map getAllpc(String cityId) {
		Map<String, Object> reMap = new HashMap<>();
		// TODO Auto-generated method stub
		/*List<OperatorWelcomes> OperatorWelcomeses=operatorWelcomesRepository.findAll();
		if(OperatorWelcomeses.size()>0){
		reMap.put("OperatorWelcomeses", OperatorWelcomeses.get(0));}*/
		OperatorHotwords operatorHotwords=operatorHotwordsRepository.findPcByCityCodeOne(cityId);
		reMap.put("hot", operatorHotwords);
		List<OperatorHotwords> hots=operatorHotwordsRepository.findPcByCityCodetwo(cityId);
		reMap.put("hots", hots);
		
		List<OperatorRoundPhoto> OperatorRoundPhotos=operatorRoundPhotoRepository.findBycityCode(cityId);
		System.out.println(OperatorRoundPhotos.size()+"轮播图数量");
		reMap.put("OperatorRoundPhotos", OperatorRoundPhotos);
		List<OperatorOfficeSpaceRo> OperatorOfficeSpaceRos=new ArrayList<OperatorOfficeSpaceRo>();
		List<OperatorOfficeSpace> officeSpaces=operatorOfficeSpaceRepository.getOperatorOfficeSpace(cityId);
		if(officeSpaces.size()>0){
		for(OperatorOfficeSpace office:officeSpaces){
			OperatorOfficeSpaceRo ofro=new OperatorOfficeSpaceRo();
			ProvideRoom pr=operatorRoomRepository.findByroomCodes(office.getRoomCode());
			if(pr!=null){
			ofro.setRoomCode(office.getRoomCode());
			ofro.setPicture(pr.getRoomlogo());
			ofro.setDetailss(pr.getDescribeAsk());
			ofro.setRoomName(pr.getRoomName());
			OperatorOfficeSpaceRos.add(ofro);
			}
		}
		}
		reMap.put("OperatorOfficeSpaceRos", OperatorOfficeSpaceRos);
		
		List<OperatorPcCompanyDevelop> opds=operatorPcCompanyDevelopRepository.getOperatorPcCompanyDevelops(cityId);
		if(opds.size()>0){
		for(OperatorPcCompanyDevelop opd:opds){
			OperatorCompanyDevelopServer[] depservers=operatorCompanyDevelopServerRepository.getOperatorCompanyDevelopServers(opd.getId());
			for(OperatorCompanyDevelopServer depserver:depservers){
				OperatorServerClass operatorServerClass=operatorServerClassRepository.findOne(depserver.getTrueServerId());
				if(null!=operatorServerClass){
				depserver.setServerName(operatorServerClass.getServerName());
				depserver.setServerPic(operatorServerClass.getServerPicUrl());}
				
			}
			opd.setOperatorCompanyDevelopServers(depservers);
		}
		}
		reMap.put("opdevelop", opds);
		
		List<OperatorPcActive>  pcActives=operatorPcActiveRepository.getOperatorPcActive(cityId);
		List<OperatorPcActiveRo> pcActiveRos=new ArrayList<OperatorPcActiveRo>();
		if(pcActives.size()>0){
		for(OperatorPcActive pcActive:pcActives){
			OperatorPcActiveRo active=new OperatorPcActiveRo();
			OperatorEventDetails operatorEventDetails=operatorEventDetailsRepository.getSelectEventByid(pcActive.getActiveId());
			if(null!=operatorEventDetails&&!operatorEventDetails.equals("")){
				active.setActiveid(pcActive.getActiveId());
				active.setActiveName(operatorEventDetails.getEventTitle());
				active.setIsMoney(operatorEventDetails.getIsMoney());
				active.setPicture(operatorEventDetails.getEventPicUrl());
				active.setStartDate(operatorEventDetails.getEventStartTime());
				pcActiveRos.add(active);
			}
		}
		}
		reMap.put("pcActiveRos", pcActiveRos);
		List<OperatorHotBusiness> hotbusinessList=operatorHotBusinessRepository.getOperatorHotBusiness(cityId);
		List<OperatorHotBusinessRo>  OperatorHotBusinessRos=new ArrayList<OperatorHotBusinessRo>();
		if(hotbusinessList.size()>0){
		for(OperatorHotBusiness hotbusiness:hotbusinessList){
			
			List<ProviderBusiness> pros=providerBusinessRepository.selectBusinessAndUser(hotbusiness.getBusNumber());
			if(pros.size()>0){
				ProviderBusiness	pro=pros.get(0);
				OperatorHotBusinessRo operatorHotBusinessRo=new OperatorHotBusinessRo();
				operatorHotBusinessRo.setBusNumber(hotbusiness.getBusNumber());
				operatorHotBusinessRo.setCompanyName(pro.getCompanyName());
				operatorHotBusinessRo.setPicture(hotbusiness.getBusPicture());
				OperatorHotBusinessRos.add(operatorHotBusinessRo);
			}
		}
		}
		reMap.put("OperatorHotBusinessRos", OperatorHotBusinessRos);
		List<OperatorFriendshipLink> friendshiplinks=operatorFriendshipLinkRepository.findByType("1");
		reMap.put("friendshiplinks", friendshiplinks);
		List<AppOutServerClass> appsBusinessService=new ArrayList();//企业服务
		List<OperatorServerClass> ServerClassList=operatorServerClassRepository.findListBypcNavigation("1");
		for(OperatorServerClass ser:ServerClassList){
			AppOutServerClass  appOutServerClass=new AppOutServerClass();
			List<OperatorServerClass> serclasses=operatorServerClassRepository.findByParentServerId(ser.getId());//二级
			appOutServerClass.setOperatorServerClass(ser);
			if(serclasses.size()>0){
				List<PctwoServerClass> pcs=new ArrayList();
				for(OperatorServerClass sertwoclass:serclasses){
					PctwoServerClass pctwoServerClass=new PctwoServerClass();
					List<OperatorServerClass> serthrees=operatorServerClassRepository.findByParentServerId(sertwoclass.getId());
					pctwoServerClass.setOperatorSerTwo(sertwoclass);
					if(serthrees.size()>0){
					pctwoServerClass.setServerClasThree(serthrees);}
					pcs.add(pctwoServerClass);
				}
				appOutServerClass.setServerClasTwo(pcs);
			}
			appsBusinessService.add(appOutServerClass);
		}
		reMap.put("appsBusinessService", appsBusinessService);
		List<AppOutServerClass> appsknowledgeService=new ArrayList();//知识产权服务
		List<OperatorServerClass> ServerknowledgeClassList=operatorServerClassRepository.findListBypcNavigation("2");
		for(OperatorServerClass ser:ServerknowledgeClassList){
			AppOutServerClass  appOutServerClass=new AppOutServerClass();
			List<OperatorServerClass> serclasses=operatorServerClassRepository.findByParentServerId(ser.getId());//二级
			appOutServerClass.setOperatorServerClass(ser);
			if(serclasses.size()>0){
				List<PctwoServerClass> pcs=new ArrayList();
				for(OperatorServerClass sertwoclass:serclasses){
					PctwoServerClass pctwoServerClass=new PctwoServerClass();
					List<OperatorServerClass> serthrees=operatorServerClassRepository.findByParentServerId(sertwoclass.getId());
					pctwoServerClass.setOperatorSerTwo(sertwoclass);
					if(serthrees.size()>0){
					pctwoServerClass.setServerClasThree(serthrees);}
					pcs.add(pctwoServerClass);
				}
				appOutServerClass.setServerClasTwo(pcs);
			}
			appsknowledgeService.add(appOutServerClass);
		}
		reMap.put("appsknowledgeService", appsknowledgeService);
		List<AppOutServerClass> appstrainService=new ArrayList();//培训服务
		List<OperatorServerClass> ServertrainClassList=operatorServerClassRepository.findListBypcNavigation("3");
		for(OperatorServerClass ser:ServertrainClassList){
			AppOutServerClass  appOutServerClass=new AppOutServerClass();
			List<OperatorServerClass> serclasses=operatorServerClassRepository.findByParentServerId(ser.getId());//二级
			appOutServerClass.setOperatorServerClass(ser);
			if(serclasses.size()>0){
				List<PctwoServerClass> pcs=new ArrayList();
				for(OperatorServerClass sertwoclass:serclasses){
					PctwoServerClass pctwoServerClass=new PctwoServerClass();
					List<OperatorServerClass> serthrees=operatorServerClassRepository.findByParentServerId(sertwoclass.getId());
					pctwoServerClass.setOperatorSerTwo(sertwoclass);
					if(serthrees.size()>0){
					pctwoServerClass.setServerClasThree(serthrees);}
					pcs.add(pctwoServerClass);
				}
				appOutServerClass.setServerClasTwo(pcs);
			}
			appstrainService.add(appOutServerClass);
		}
		reMap.put("appstrainService", appstrainService);
		List<AppOutServerClass> appsspaceService=new ArrayList();//空间服务
		List<OperatorServerClass> ServerspaceClassList=operatorServerClassRepository.findListBypcNavigation("5");
		for(OperatorServerClass ser:ServerspaceClassList){
			AppOutServerClass  appOutServerClass=new AppOutServerClass();
			List<OperatorServerClass> serclasses=operatorServerClassRepository.findByParentServerId(ser.getId());//二级
			appOutServerClass.setOperatorServerClass(ser);
			if(serclasses.size()>0){
				List<PctwoServerClass> pcs=new ArrayList();
				for(OperatorServerClass sertwoclass:serclasses){
					PctwoServerClass pctwoServerClass=new PctwoServerClass();
					List<OperatorServerClass> serthrees=operatorServerClassRepository.findByParentServerId(sertwoclass.getId());
					pctwoServerClass.setOperatorSerTwo(sertwoclass);
					if(serthrees.size()>0){
					pctwoServerClass.setServerClasThree(serthrees);}
					pcs.add(pctwoServerClass);
				}
				appOutServerClass.setServerClasTwo(pcs);
			}
			appsspaceService.add(appOutServerClass);
		}
		reMap.put("appsspaceService", appsspaceService);
		return reMap;
	}
	
	public List<AppOutServerClass> selectOperatorServerClassList(String pcNavigation){
		List<AppOutServerClass> apps=new ArrayList();
		List<OperatorServerClass> ServerClassList=operatorServerClassRepository.findListBypcNavigation(pcNavigation);
		for(OperatorServerClass ser:ServerClassList){
			AppOutServerClass  appOutServerClass=new AppOutServerClass();
			List<OperatorServerClass> serclasses=operatorServerClassRepository.findByParentServerId(ser.getId());//二级
			appOutServerClass.setOperatorServerClass(ser);
			if(serclasses.size()>0){
				List<PctwoServerClass> pcs=new ArrayList();
				for(OperatorServerClass sertwoclass:serclasses){
					PctwoServerClass pctwoServerClass=new PctwoServerClass();
					List<OperatorServerClass> serthrees=operatorServerClassRepository.findByParentServerId(sertwoclass.getId());
					pctwoServerClass.setOperatorSerTwo(sertwoclass);
					if(serthrees.size()>0){
					pctwoServerClass.setServerClasThree(serthrees);}
					pcs.add(pctwoServerClass);
				}
				appOutServerClass.setServerClasTwo(pcs);
			}
			apps.add(appOutServerClass);
		}
		return apps; 
	}
	
	
	public List<AppOutServerClass> selectOperatorServerClassListNoPra(){
		List<AppOutServerClass> apps=new ArrayList();
		List<OperatorServerClass> ServerClassList=operatorServerClassRepository.selectByOneServerclass();
		for(OperatorServerClass ser:ServerClassList){
			AppOutServerClass  appOutServerClass=new AppOutServerClass();
			List<OperatorServerClass> serclasses=operatorServerClassRepository.findByParentServerId(ser.getId());//二级
			appOutServerClass.setOperatorServerClass(ser);
			if(serclasses.size()>0){
				List<PctwoServerClass> pcs=new ArrayList();
				for(OperatorServerClass sertwoclass:serclasses){
					PctwoServerClass pctwoServerClass=new PctwoServerClass();
					List<OperatorServerClass> serthrees=operatorServerClassRepository.findByParentServerId(sertwoclass.getId());
					pctwoServerClass.setOperatorSerTwo(sertwoclass);
					if(serthrees.size()>0){
					pctwoServerClass.setServerClasThree(serthrees);}
					pcs.add(pctwoServerClass);
				}
				appOutServerClass.setServerClasTwo(pcs);
			}
			apps.add(appOutServerClass);
		}
		return apps; 
	}

	

}
